<!DOCTYPE html>
<html>
  <head>
    <title>图纸名称</title>
    <meta charset="UTF-8" />
    <link rel="shortcut icon" href="custom/images/favicon.ico?v=2" />
    <style>
      html,
      body {
        padding: 0px;
        margin: 0px;
      }
    </style>
    <script src='../libs/jquery-3.4.1.min.js'></script>

    <script src='../libs/core/IZrowIfUVaXF.js'></script>
    <script src='../libs/core/VT3MIEkV8AKv.js'></script>
    <script src='../libs/core/ht.js'></script>
    <script src='../libs/core/buckle.js'></script>
    <script src='../libs/plugin/ht-edgetype.js'></script>
    <script src="../libs/plugin/ht-obj.js"></script>
    <script src='../libs/core/ht-ui.js'></script>
    <script src='../libs/plugin/ht-form.js'></script>
    <script src='../libs/plugin/ht-vector.js'></script>
     <script src="../libs/axios.min.js"></script>

    <script src="../libs/plugin/common.js"></script>
	<script src="../libs/echarts.js"></script>
	<script src="../configs/urlConfig.js"></script>

    <script>
      //定义全局变量
      // var attributeConfig = {
      //   url: "http://172.16.11.150"
      // };
      var startTime;
      var endTime;
      //日期对象
      var date = new Date();
      var nowDay = date.getDay();
      var newDm;
      var select01_key;
      var select02_key;

      //日月季度年
      var showType = "day";
      //创建全局变量url
      var canshu;
      //监听url相关的全局变量
      var a = {
        watchValue: 1
      };
      var lastTimeValue = a.watchValue;
      var channelCodeValue01;
      var channelCode_01Value02;
      var channelCode_01;

      var arrKey = [];
      var arrValue = [];

      function init() {
        dataModel = new ht.DataModel();

        graphView = new ht.graph.GraphView(dataModel);
        graphView.addToDOM();

        // 选中边框为0
        graphView.getSelectWidth = function() {
          return 0;
        };
        // 禁止图元移动
        graphView.setMovableFunc(function() {
          return false;
        });

        graphView.deserialize(
          "displays/冀北/消纳/jb_xiaonajianshi.json",
          function(json, dm, g2d, datas) {
            //ht.Default.xhrLoad('displays/冀北/消纳/增发电量.json', function (text) {

            // var json = ht.Default.parse(text);
            // if (json.title) document.title = json.title;
            // console.log("加载图纸");
            //自适应屏幕
            graphView.fitContent(true);
            treeTableView = new ht.widget.TreeTableView(dataModel);
            newDm = dm;
            // //获取利用小时数
            const select01 = dataModel.getDataByTag("select01");
            select01_key = select01.a("ht.value");
            //获取时间
            const select02 = dataModel.getDataByTag("select02");
            select02_key = select02.a("ht.value");

            const channelCode = dataModel.getDataByTag("channelCode");
            channelCode_value = channelCode.a("ht.value");
            channelCode_01 = dataModel.getDataByTag("channelCode_01");
            channelCode_01Value01 = channelCode_01.a("ht.value");
            getAreaCode(undefined, channelCode);
            getMapList(select01_key, select02_key);
            //getRealtimeConsumption(channelCode, null);

            getRealtimeConsumption(channelCode, null);

            getBlockedElectricityAndCountFault(
              "?startTime=" +
                new Date(startTime).getTime() +
                "&endTime=" +
                new Date(endTime).getTime(),
              channelCode_01Value01
            );

            getZiyuanliyong(startTime, endTime, channelCode_01Value01);
            getCuAndSdp(startTime, endTime, channelCode_01Value01);

            select02.a("ht.onChange", (oldVal, newVal, data, view) => {
              select02_key = newVal;
              getMapList(select01_key, newVal);
            });
            select01.a("ht.onChange", (oldVal, newVal, data, view) => {
              select01_key = newVal;
              getMapList(newVal, select02_key);
            });

            channelCode_01.a("ht.onChange", (oldVal, newVal, data, view) => {
              channelCode_01Value01 = channelCode_01.a("ht.value");
              getZiyuanliyong(startTime, endTime, newVal);
              getCuAndSdp(startTime, endTime, newVal);

              getBlockedElectricityAndCountFault(
                "?startTime=" +
                  new Date(startTime).getTime() +
                  "&endTime=" +
                  new Date(endTime).getTime(),
                newVal
              );
            });
            channelCode.a("ht.onChange", (oldVal, newVal, data, view) => {
              channelCode_value = channelCode.a("ht.value");
              getRealtimeConsumption(channelCode, newVal);
            });
            //中间极坐标实现代码
            //获取开始结束时间
            const startDate = dataModel.getDataByTag("startDate");
            const endDate = dataModel.getDataByTag("endDate");
            time1 = getFirstDayOfMonth(new Date());
            time2 = getNowDay(new Date());
            date1 = new Date(time1);
            date2 = new Date(time2);
            startDate.a("ht.value", time1);
            endDate.a("ht.value", time2);
            startDate.a("ht.onChange", (oldVal, newVal) => {
              startTime = newVal;
              canshu =
                "?startTime=" +
                new Date(startTime).getTime() +
                "&endTime=" +
                new Date(endTime).getTime();
              a.watchValue = canshu;
            });
            endDate.a("ht.onChange", (oldVal, newVal) => {
              endTime = newVal;
              //拼接url
              canshu =
                "?startTime=" +
                new Date(startTime).getTime() +
                "&endTime=" +
                new Date(endTime).getTime();
              a.watchValue = canshu;
            });

            //监听url 变化则重新发起请求
            Object.defineProperty(a, "watchValue", {
              get: function() {
                //console.log("get：" + watchValue);
                return watchValue;
              },
              set: function(value) {
                watchValue = value;
                if (lastTimeValue != watchValue) {
                  lastTimeValue = watchValue;
                  // console.log("set：" + canshu);

                  getBlockedElectricityAndCountFault(
                    canshu,
                    channelCode_01Value01
                  );
                  getZiyuanliyong(startTime, endTime, channelCode_01Value01);
                  getCuAndSdp(startTime, endTime, channelCode_01Value01);
                }
              }
            });

            setInterval(() => {
              getRealtimeConsumption(channelCode, null);
            }, 50000);
          }
        );
      }
      var colors = [];
      //折线图shuju
      function getRealtimeConsumption(channelCode, selectValue) {
        if (selectValue == null);
        selectValue = channelCode_value;
        let i = arrValue.indexOf(selectValue);
        var key = arrKey[i];
        var consumption = dataModel.getDataByTag("consumption");
        var zhexiandata = "";

        $.ajax({
          type: "GET",
          async: false, //同步请求
          // startTime : System.currentTimeMillis() ,
          // endTime : System.currentTimeMillis(),
          url:
            attributeConfig.url +
            "/epplatform/v1/realtimeConsumption?channelCode=" +
            key,
          contentType: "application/json; charset=utf-8",
          success: function(data) {
            if (data.code == 0) {
              zhexiandata = data.data;
              if (zhexiandata != "") {
                for (var i = 0; i < zhexiandata.x.length; i++) {
                  var colorArr = gradientColor(
                    GradationColors[i],
                    GradationColors[i + 1],
                    4
                  );
                  colors = colors.concat(colorArr);
                }
                debugger;
                console.log(colors);
                consumption.a("xData", zhexiandata.x);
                consumption.a("colors", colors);
                consumption.a("yData", zhexiandata.y);
                consumption.a("names", zhexiandata.names);
                var arr = [];
                console.log(zhexiandata.x);
                zhexiandata.x.forEach(node => {
                  arr.push(Math.max.apply(null, node));
                });

               // consumption.a("warnMax", Math.max.apply(null, arr));
                consumption.a("warnMax", zhexiandata.channelLimit);
              }
            }
          },
          error: function() {
            alert("失败，请稍后再试！");
          }
        });
      }

      //获取地区接口
      function getAreaCode(channelCode, channelCode_01) {
        var data = updateDataModel("/epplatform/v1/sectionStacks");

        if (channelCode_01 != undefined) {
          channelCode_01.a("ht.dataSource", data);
        }
        if (channelCode != undefined) {
          channelCode.a("ht.dataSource", data);
        }
        data.forEach(element => {
          arrValue.push(element.value);
          arrKey.push(element.key);
        });
      }
      var arrIcon = [];
      var names = ["秦皇岛", "张家口", "廊坊", "唐山", "承德"];

      //获取地图数据
      function getMapList(select01_key, select02_key) {
        debugger;
        getElement();
        axios.all([getKey(select01_key, "1"), getKey(select02_key, "2")]).then(
          axios.spread(function(key1, key2) {
            var getUrl =
              "?" +
              "startTime=" +
              new Date(startTime).getTime() +
              "&endTime=" +
              new Date(endTime).getTime() +
              "&dataType=" +
              key1;
            //获取地图数据
            var data = updateDataModel(
              "/epplatform/v1/regionalConsumption" + getUrl
            );

            if (data.discardElectricity.length != 0 && key1 == "3") {
              //画地图节点
              createNodes(data.discardElectricity);
            }
            if (data.discardElectricityProbability.length != 0 && key1 == "2") {
              //画地图节点
              createNodes(data.discardElectricityProbability);
            }
            if (data.useOfHour.length != 0 && key1 == "1") {
              //画地图节点
              createNodes(data.useOfHour);
            }
          })
        );
      }

      //改变节点通用方法
      function getElement() {
        var names = ["秦皇岛", "张家口", "廊坊", "唐山", "承德"];
        names.forEach(element => {
          var tag = dataModel.getDataByTag(element);
          if (tag != undefined) tag.s("2d.visible", false);
        });
      }
      //资源利用
      function getZiyuanliyong(date1, date2, channelCode_01Value01) {
        getAreaCode(undefined, channelCode_01);
        let i = arrValue.indexOf(channelCode_01Value01);
        var key = arrKey[i];

        var data = updateDataModel(
          "/epplatform/v1/resourceUtilization?startTime=" +
            new Date(date1).getTime() +
            "&endTime=" +
            new Date(endTime).getTime() +
            "&channelCode=" +
            key
        );
        //console.log(data);
        const zyly = dataModel.getDataByTag("zyly");
        zyly.a("lineData1", data.theoryPowers);
        zyly.a("lineData2", data.actualPowers);
        zyly.a("xAxisData", data.months);
      }
      //受阻电量和故障频率
      function getBlockedElectricityAndCountFault(
        canshu,
        channelCode_01Value01
      ) {
        getAreaCode(undefined, channelCode_01);
        let q = arrValue.indexOf(channelCode_01Value01);
        var key = arrKey[q];
        debugger;
        canshu =
          "/epplatform/v1/blockedElectricityAndCountFault" +
          canshu +
          "&channelCode=" +
          key;
        var data = updateDataModel(canshu);

        var fault = dataModel.getDataByTag("fault");
        var hinderedRate = dataModel.getDataByTag("hinderedRate");

        hinderedRate.a("xnames", data.blockedElectricity.indicator);

        hinderedRate.a("data1", data.blockedElectricity.data);

        fault.a("xnames", data.faultCount.indicator);
        fault.a("data1", data.faultCount.data);
        console.log(fault.a("data1"));
        console.log(fault.a("xnames"));
      }
      //通道利用率  断面弃用率
      function getCuAndSdp(startTime, endTime, channelCode_01Value01) {
        getAreaCode(undefined, channelCode_01);

        let q = arrValue.indexOf(channelCode_01Value01);
        var key = arrKey[q];
        var powerUtilization = dataModel.getDataByTag("powerUtilization"); //通道利用率
        var powerFailure = dataModel.getDataByTag("powerFailure"); //断面弃用率
        var areaCode = dataModel.getDataByTag("areaCode"); //未定
        var data = updateDataModel(
          "/epplatform/v1/cuAndSdp?" +
            "startTime=" +
            new Date(startTime).getTime() +
            "&endTime=" +
            new Date(endTime).getTime() +
            "&channelCode=" +
            key
        );
        for (var i = 0; i < data.length; i++) {
          if (data[i].key == "断面弃电率") {
            powerFailure.a(
              "value",
              data[i].value == null ? "--" : data[i].value
            );
          }
          if (data[i].key == "通道利用率") {
            powerUtilization.a(
              "value",
              data[i].value == null ? "--" : data[i].value
            );
          }
        }
      }

      //循环取值
      function getKey(value, type) {
        var key;
        if (type == "1") {
          switch (value) {
            case "利用小时数(h)": //
              key = "1";
              break;
            case "弃电率":
              key = "2";
              break;
            case "弃电量":
              key = "3";
              break;
            default:
          }
        } else {
          switch (value) {
            case "年累计": //
              key = "1";
              startTime = getFirstDayOfYear(new Date());
              endTime = getNextToLastDay(new Date());
              break;
            case "月累计":
              key = "2";
              startTime = getFirstDayOfMonth(new Date());
              endTime = getNextToLastDay(new Date());
              break;
            default:
          }
        }
        return key;
      }

      //发起请求数据通用接口
      function updateDataModel(geturl) {
        var getValues = "";
        $.ajax({
          type: "GET",
          async: false, //同步请求
          // startTime : System.currentTimeMillis() ,
          // endTime : System.currentTimeMillis(),
          url: attributeConfig.url + geturl,
          contentType: "application/json; charset=utf-8",
          success: function(data) {
            if (data.code == 0) {
              getValues = data.data;
            }
          },
          error: function() {
            alert("失败，请稍后再试！");
            alert("失败，请稍后再试！");
          }
        });

        return getValues;
      }
      //创建node
      function createNodes(data) {
        //获取数组中的最大值
        getElement();
        var max = getMaxNum(data);

        for (var i = 0; i < data.length; i++) {
          // node.setLayer("2");
          var temp = data[i];
          var node = dataModel.getDataByTag(temp.area);
          //发光环柱子
          var value = temp.value;
          var zhuzi_heigth =
            value.substring(0, value.length - 1) /
            max.substring(0, max.length - 1);
          //  node.setName(temp.area); //设置高度缩放
          // node.setTag(temp.area);
          node.a("value", temp.value);
          node.a("caijie", zhuzi_heigth);
          // node.setDisplayName(temp
          //   .area); //
          // node.setId(temp.area);
          node.s("2d.visible", true); //可见
          node.s("interactive", true);
        }
      }

      function getMaxNum(datas) {
        // console.log(datas); //获取 //创建数组并赋值
        var strNum = [];
        for (var i = 0; i < datas.length; i++) {
          // console.log(datas[i]);
          strNum.push(datas[i].value);
        } //通过获取数组变量和原来的数组做对比
        var max = strNum[0]; //对比结果是最大值
        var min = strNum[0]; //对比结果是最小值 //通过for循环获取对比次数
        for (var i = 0; i < strNum.length; i++) {
          // // 最大值
          if (max < strNum[i]) {
            //通过if判断把最大值赋给max;
            max = strNum[i];
          } // //最小值
          if (min > strNum[i]) {
            //通过if判断把最小值赋给min；
            min = strNum[i];
          }
        }

        return max;
      }
    </script>
  </head>

  <body onload="init()"></body>
</html>
